<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        function exists(){
            var uname = document.getElementById('uname');
            var _uname = uname.value;
            var xhr = new XMLHttpRequest();
            console.log(uname.value)
            if(!_uname){
                alert("用户名不能为空");
                return;
            }
            xhr.onreadystatechange = function(){
                if(xhr.readyState==4 && xhr.status==200){
                    var rs = xhr.responseText;
                    if(rs==1){
                        alert('用户名存在');
                        return;
                    }else{
                        alert('欢迎使用')
                    }
                }
            }
            var url = `http://127.0.0.1:8080/pro/v1/exists?uname=${_uname}`
            xhr.open('get',url,true);
            xhr.send();
        }
        function reg(){
            var uname = document.getElementById("uname");
            var upwd = document.getElementById("upwd");
            var _uname = uname.value
            var _upwd = upwd.value;
            if(!_upwd){
                alert("密码不能为空");
                return;
            }
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(){
                if(xhr.readyState==4 && xhr.status==200){
                    var rs = xhr.responseText;
                    if(rs==1){
                        alert('注册成功');
                    }else if(re==0){
                        alert('注册失败');
                    }
                }
            };
            const url = "http://127.0.0.1:8080/pro/v1/reg";
            xhr.open("POST",url,true)
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xhr.send(`uname=${_uname}&upwd=${_upwd}`);
        }
    </script>
    <form>
        <input type="text" name="uname" value="" id="uname" placeholder="用户名" onblur="exists()">
        <input type="password" name="upwd" id="upwd" value="">
        <input type="button" onclick="reg()" value="注册">
    </form>
</body>
</html>